package com.tkjt.pms.modules.scoring.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;

import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tkjt.pms.common.entity.*;
import com.tkjt.pms.modules.common.mapper.*;
import com.tkjt.pms.modules.report.util.DeptConstant;
import com.tkjt.pms.server.common.DepartUserChangeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkjt.pms.calc.common.DepartOwnerInfo;
import com.tkjt.pms.common.utils.DepartCode;
import com.tkjt.pms.common.utils.DoubleUtil;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.EsJsonUtils;
import com.tkjt.pms.common.utils.IndexCommonRepositories;
import com.tkjt.pms.common.utils.InfoAttrUtil;
import com.tkjt.pms.common.utils.TkGradeUtil;
import com.tkjt.pms.common.utils.TkIndexUtils;
import com.tkjt.pms.common.vos.RequestJson;
import com.tkjt.pms.modules.kpiadmin.mapper.DataGridMapper;
import com.tkjt.pms.modules.scoring.mapper.TkScoringMapper;
import com.tkjt.pms.modules.scoring.model.GradeDetailDatagridResponseModel;
import com.tkjt.pms.modules.scoring.model.GradeViewDatagridResponseModel;
import com.tkjt.pms.modules.scoring.model.ScoreCommentQueryRequestModel;
import com.tkjt.pms.modules.scoring.model.ScoreCommentQueryResponseModel;
import com.tkjt.pms.modules.scoring.model.ScoreCommentSaveRequestModel;
import com.tkjt.pms.modules.scoring.model.ScoreDatagridRequestModel;
import com.tkjt.pms.modules.scoring.model.ScoreDatagridResponseModel;
import com.tkjt.pms.modules.scoring.model.ScoreValueSaveRequestModel;
import com.tkjt.pms.modules.scoring.service.TkScoringBizService;
import com.tkjt.pms.modules.scoring.vo.RecordsBean;
import com.tkjt.pms.server.common.DepartAndInfoTool;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Transactional
public abstract class TkScoringBizServiceBase implements TkScoringBizService {
    @Autowired
    private TkIndexValueMapper tkIndexValueMapper;
    @Autowired
    private TkIndexValueAttrsMapper tkIndexValueAttrsMapper;
    @Autowired
    private TkIndexInfoMapper tkIndexInfoMapper;
    @Autowired
    private TkIndexValueAuditMapper tkIndexValueAuditMapper;
    @Autowired
    private TkIndexValueCommentsMapper tkIndexValueCommentsMapper;
    @Autowired
    private TkScoringMapper tkScoringMapper;
    @Autowired
    private PMSCommonMapper commonMapper;
//    @Autowired
//    private TkIndexReportCtrlMapper tkIndexReportCtrlMapper;
    @Autowired
    private TkIndexValueMirrorMapper tkIndexValueMirrorMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private DepartAndInfoTool departAndInfoTool;
    @Autowired
    private DataGridMapper dataGridMapper;
    @Autowired
    private SysDepartMirrorMapper sysDepartMirrorMapper;
    @Autowired
    private DepartUserChangeService departUserChangeService;
    @Override
    public Result<?> indexCodes(LoginUser loginUser) {
        List<String> infoAttrs = new ArrayList<>();
        infoAttrs.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
        List<TkIndexInfo> infos = departAndInfoTool.getInfosByParam(loginUser.getOrgCode(),infoAttrs);
        List<Map<String, Object>> list = new ArrayList<>();
        for (TkIndexInfo info : infos){
            Map<String,Object> param = new HashMap<>();
            param.put("name",info.getIndexName());
            param.put("code",info.getIndexCode());
            list.add(param);
        }
        return Result.ok(list);
    }

    @Override
    public Result<?> scoreDatagrid(ScoreDatagridRequestModel model,String dataType) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> indexOrgs = model.getIndexOrgs();//非必填
        List<String> scoreIndexs = model.getIndexCodes();//非必填
        String yearMonth = model.getIndexYearMonth();
        int pageNo = model.getPageNo() == 0? 1 :model.getPageNo();
        int pageSize =  model.getPageSize() == 0? 10 :model.getPageSize();
        String year = yearMonth.substring(0, 4);
        String month = yearMonth.substring(4);
        QueryWrapper<TkIndexValue> queryWrapper=new QueryWrapper<>();
		if (indexOrgs != null && indexOrgs.size() != 0) {
			queryWrapper.in("INDEX_DEPART", indexOrgs);
		}
		if(null == scoreIndexs) {
			scoreIndexs = new ArrayList<String>();
		}
		if(scoreIndexs.isEmpty()) {
            List<String> infoAttrs = new ArrayList<>();
            infoAttrs.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
            List<String> tempIndexCodes = departAndInfoTool.getIndexCodesByParam(loginUser.getOrgCode(),infoAttrs);
            scoreIndexs.addAll(tempIndexCodes);
		}
        if(scoreIndexs.isEmpty()){
        	scoreIndexs.add("");
        }
        queryWrapper.in("INDEX_CODE",scoreIndexs);
        queryWrapper.eq("INDEX_EXECUTOR",loginUser.getUsername());
        queryWrapper.eq("INDEX_EXECUTOR_DEPART",loginUser.getOrgCode());
        queryWrapper.eq("YEAR",year);
        queryWrapper.eq("DATA_TYPE",dataType);
        Page<TkIndexValue> page = new Page<>(pageNo, pageSize);
        IPage<TkIndexValue> pageList = tkIndexValueMapper.selectPage(page, queryWrapper);
        List<TkIndexValue> list = pageList.getRecords();
        ScoreDatagridResponseModel responseModel=new ScoreDatagridResponseModel();
        List<ScoreDatagridResponseModel.RecordsBean> records=new ArrayList<>();
        for (TkIndexValue tkIndexValue : list) {
            TkIndexInfo tkIndexInfo = tkIndexInfoMapper.selectOne(new QueryWrapper<TkIndexInfo>().eq("INDEX_VALUE_CODE", tkIndexValue.getIndexValueCode()));
            ScoreDatagridResponseModel.RecordsBean recordsBean=new ScoreDatagridResponseModel.RecordsBean();
            recordsBean.setIndexCode(tkIndexInfo.getIndexCode());
            recordsBean.setIndexName(tkIndexInfo.getIndexName());
            recordsBean.setIndexValueCode(tkIndexInfo.getIndexValueCode());
            recordsBean.setIndexOrgCode(tkIndexValue.getIndexDepart());//字段名要统一：indexDepart (被评分机构)
            recordsBean.setIndexOrgName(sysDepartMapper.selectOne(new QueryWrapper<SysDepart>().eq("org_code",tkIndexValue.getIndexDepart())).getDepartName());
            recordsBean.setScorerCode(tkIndexValue.getIndexScorer());
            SysUser sysUser = sysUserMapper.getUserByName(tkIndexValue.getIndexScorer());
            recordsBean.setScorerName(sysUser.getRealname());
            recordsBean.setScoreValue(formateForDoubleScale0(TkGradeUtil.createGrade(tkIndexValue, month)));
            recordsBean.setM01(formateForDoubleScale0(tkIndexValue.getM01()));
            recordsBean.setM02(formateForDoubleScale0(tkIndexValue.getM02()));
            recordsBean.setM03(formateForDoubleScale0(tkIndexValue.getM03()));
            recordsBean.setM04(formateForDoubleScale0(tkIndexValue.getM04()));
            recordsBean.setM05(formateForDoubleScale0(tkIndexValue.getM05()));
            recordsBean.setM06(formateForDoubleScale0(tkIndexValue.getM06()));
            recordsBean.setM07(formateForDoubleScale0(tkIndexValue.getM07()));
            recordsBean.setM08(formateForDoubleScale0(tkIndexValue.getM08()));
            recordsBean.setM09(formateForDoubleScale0(tkIndexValue.getM09()));
            recordsBean.setM10(formateForDoubleScale0(tkIndexValue.getM10()));
            recordsBean.setM11(formateForDoubleScale0(tkIndexValue.getM11()));
            recordsBean.setM12(formateForDoubleScale0(tkIndexValue.getM12()));
            //查评分意见
            QueryWrapper<TkIndexValueComments> queryWrapperSdf = new QueryWrapper<>();
            queryWrapperSdf.lambda().eq(TkIndexValueComments::getIndexValueId, tkIndexValue.getId()).eq(TkIndexValueComments::getIndexValueM,month);
            List<TkIndexValueComments> tkIndexValueCommentsEntitys = tkIndexValueCommentsMapper.selectList(queryWrapperSdf);
            List<ScoreDatagridResponseModel.RecordsBean.ScoreCommentBean> scoreComments=new ArrayList<>();
            if (tkIndexValueCommentsEntitys != null && tkIndexValueCommentsEntitys.size() > 0) {
                for (TkIndexValueComments t : tkIndexValueCommentsEntitys) {
                    ScoreDatagridResponseModel.RecordsBean.ScoreCommentBean scoreCommentBean=new ScoreDatagridResponseModel.RecordsBean.ScoreCommentBean();
                    scoreCommentBean.setTitle(t.getIndexValueCommentTitle());
                    scoreCommentBean.setContent(t.getIndexValueComments());
                    scoreCommentBean.setCode(t.getIndexValueCommentCode());
                    scoreComments.add(scoreCommentBean);
                }
            }
            recordsBean.setScoreComments(scoreComments);

            records.add(recordsBean);
        }
        responseModel.setRecords(records);
        responseModel.setCurrent(pageList.getCurrent());
        responseModel.setPages(pageList.getPages());
        responseModel.setSize(pageList.getSize());
        responseModel.setTotal(pageList.getTotal());
        return Result.ok(responseModel);
    }

    @Override
    public Result<?> saveRows(ScoreValueSaveRequestModel model, LoginUser loginUser,String dataType) {
        String indexYearMonth = model.getIndexYearMonth();
        String year=indexYearMonth.substring(0,4);
        String month=indexYearMonth.substring(4);
        List<TkIndexValue> tkIndexValues = new ArrayList<>();
        for (ScoreValueSaveRequestModel.ValuesBean valuesBean : model.getValues()){
            TkIndexValue indexValue=new TkIndexValue();
            indexValue.setYearmonth(indexYearMonth);
            indexValue.setIndexCode(valuesBean.getIndexCode());
            indexValue.setIndexValueCode(valuesBean.getIndexValueCode());
            indexValue.setIndexDepart(valuesBean.getIndexDepart());
            indexValue.setIndexScorer(valuesBean.getScorerCode());
            if(valuesBean.getScoreValue()==null) {
                indexValue.setGrade(0.0D);
            } else {
                indexValue.setGrade(valuesBean.getScoreValue());
            }
            tkIndexValues.add(indexValue);
        }
        String indexGroup = tkIndexValues.get(0).getIndexDepart().substring(0,9);
        QueryWrapper<TkIndexValueAudit> queryWrapperSdf = new QueryWrapper<>();
        queryWrapperSdf.lambda().eq(TkIndexValueAudit::getIndexGroup, indexGroup);
        queryWrapperSdf.lambda().eq(TkIndexValueAudit::getIndexValueY, year);
        queryWrapperSdf.lambda().eq(TkIndexValueAudit::getIndexValueM, month);
        queryWrapperSdf.lambda().eq(TkIndexValueAudit::getDataType, dataType);
        TkIndexValueAudit indexValueAuditEntity = tkIndexValueAuditMapper.selectOne(queryWrapperSdf);
        if (indexValueAuditEntity != null) {
            if("2".equals(indexValueAuditEntity.getAuditStatus())){
                return Result.error("部门指标正在审核中，不允许修改");
            }else if("3".equals(indexValueAuditEntity.getAuditStatus())){
                return Result.error("部门指标已审核，不允许修改");
            }

        }
        saveFinalRows(tkIndexValues,loginUser,dataType);
        return Result.ok();
    }

    @Override
    public Result<?> scoreCommentSave(LoginUser loginUser, ScoreCommentSaveRequestModel model,String dataType) throws Exception {
        String yearMonth = model.getIndexYearMonth();
        String year=yearMonth.substring(0,4);
        String month=yearMonth.substring(4);
        String indexDepart = model.getIndexDepart();
        String indexValueCode = model.getIndexValueCode();
        String scorerCode = model.getScorerCode();
        String scoreValue = model.getScoreValue();
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        QueryWrapper<TkIndexValueAudit> auditQuery = new QueryWrapper<>();
        auditQuery.lambda().eq(TkIndexValueAudit::getIndexGroup, indexDepart.substring(0,9));
        auditQuery.lambda().eq(TkIndexValueAudit::getIndexValueY, currentYearMonth.substring(0,4));
        auditQuery.lambda().eq(TkIndexValueAudit::getIndexValueM, currentYearMonth.substring(4));
        auditQuery.lambda().eq(TkIndexValueAudit::getDataType, dataType);
        TkIndexValueAudit indexValueAuditEntity = tkIndexValueAuditMapper.selectOne(auditQuery);
        if (indexValueAuditEntity != null) {
            if("2".equals(indexValueAuditEntity.getAuditStatus())){
                return Result.error("部门指标正在审核中，不允许修改");
            }else if("3".equals(indexValueAuditEntity.getAuditStatus())){
                return Result.error("部门指标已审核，不允许修改");
            }

        }
        List<ScoreCommentSaveRequestModel.ScoreCommentsBean> scoreComments = model.getScoreComments();

        QueryWrapper<TkIndexValue> queryWrapperSdf = new QueryWrapper<>();
        queryWrapperSdf.eq("INDEX_EXECUTOR",loginUser.getUsername());
        queryWrapperSdf.eq("INDEX_EXECUTOR_DEPART",loginUser.getOrgCode());
        queryWrapperSdf.eq("INDEX_VALUE_CODE",indexValueCode);
        queryWrapperSdf.eq("INDEX_DEPART",indexDepart);
        queryWrapperSdf.eq("INDEX_SCORER",scorerCode);
        queryWrapperSdf.eq("DATA_TYPE",dataType);
        TkIndexValue indexValue = tkIndexValueMapper.selectOne(queryWrapperSdf);
        if(indexValue == null){
            throw new Exception("indexError",new Throwable("指标无效:"+indexValueCode));
        }
        indexValue.setGrade(new Double(scoreValue));
        TkGradeUtil.updateGrade(indexValue,indexValue.getGrade(),month);
        tkIndexValueMapper.updateById(indexValue);
        if(scoreComments == null){ return Result.ok();}
        QueryWrapper<TkIndexValueComments> queryCommentsEntity = new QueryWrapper<>();
        queryCommentsEntity.eq("INDEX_VALUE_ID",indexValue.getId());
        queryCommentsEntity.eq("INDEX_VALUE_M",month);
        tkIndexValueCommentsMapper.delete(queryCommentsEntity);
        int sunTotal=0;
        for (ScoreCommentSaveRequestModel.ScoreCommentsBean valuesBean : scoreComments){
            sunTotal++;
            TkIndexValueComments tkIndexValueComments = new TkIndexValueComments();
            tkIndexValueComments.setIndexValueId(indexValue.getId());
            tkIndexValueComments.setIndexValueM(month);
            tkIndexValueComments.setIndexValueCommentSeq(sunTotal);
            tkIndexValueComments.setIndexValueCommentTitle(valuesBean.getTitle());
            tkIndexValueComments.setIndexValueCommentCode(valuesBean.getCode());
            tkIndexValueComments.setIndexValueComments(valuesBean.getContent());
            tkIndexValueCommentsMapper.insert(tkIndexValueComments);
            log.info("tk_index_value_comments评分意见成功添加:"+sunTotal);
        }
        return Result.ok();
    }

    @Override
    public GradeViewDatagridResponseModel gradeViewDatagrid(String year,String dataType) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userName = loginUser.getUsername();
        List<String> orgCodes = tkScoringMapper.selectOrgCodesByUserName(userName);
        List<String> reportOrgCodes = tkScoringMapper.selectReportOrgCodesByUserName(userName);
        //List<String> orgCodes = tkIndexReportCtrlMapper.getOrgCodeAllow(loginUser.getOrgCode());

        QueryWrapper<TkIndexValue> qw = new QueryWrapper<>();
        List<String> rcurrentUserRoleCodes = tkScoringMapper.selectRolesByUserName(userName);

        if(reportOrgCodes.size() > 0){
            if(!rcurrentUserRoleCodes.contains("ykgm") && !rcurrentUserRoleCodes.contains("ykaudit") && !rcurrentUserRoleCodes.contains("ykkpiowner")){
                qw.and(wrapper -> wrapper.in("INDEX_DEPART", orgCodes.toArray()).eq("INDEX_SCORER",userName).or().in("INDEX_DEPART", reportOrgCodes.toArray()));
            }else{
                qw.and(wrapper -> wrapper.in("INDEX_DEPART", orgCodes.toArray()).or().in("INDEX_DEPART", reportOrgCodes.toArray()));
            }
        }else{
            if(!rcurrentUserRoleCodes.contains("ykgm") && !rcurrentUserRoleCodes.contains("ykaudit") && !rcurrentUserRoleCodes.contains("ykkpiowner")){
                qw.in("INDEX_DEPART", orgCodes.toArray()).eq("INDEX_SCORER",userName);
            }else{
                qw.in("INDEX_DEPART", orgCodes.toArray());
            }
        }
        List<String> infoAttrs = new ArrayList<>();
        infoAttrs.add(InfoAttrUtil.ZONG_JI);
        List<String> indexValueCodes = departAndInfoTool.getIndexValueCodesByParam(null,infoAttrs);
        qw.in("INDEX_VALUE_CODE",indexValueCodes );
        qw.eq("DATA_TYPE",dataType);
        List<TkIndexValue> list = tkIndexValueMapper.selectList(qw);
        GradeViewDatagridResponseModel model=new GradeViewDatagridResponseModel();
        List<GradeViewDatagridResponseModel.RecordsBean> records = new ArrayList<>();

        String yearMonth = EsDateUtils.getIndexYearMonth(new Date());
        for (TkIndexValue item : list) {
            GradeViewDatagridResponseModel.RecordsBean recordsBean=new GradeViewDatagridResponseModel.RecordsBean();
            recordsBean.setIndexOrgCode(item.getIndexDepart());
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", item.getIndexDepart());
            recordsBean.setIndexOrgName(sysDepartMapper.selectOne(sysDepartQueryWrapper).getDepartName());
            recordsBean.setScorerCode(item.getIndexScorer());
            String indexScorer = item.getIndexScorer();
            if(indexScorer != null){
                SysUser user = sysUserMapper.getUserByName(indexScorer);
                if(user != null){
                    recordsBean.setScorerName(user.getRealname());
                }
            }
            String indexValueCode = item.getIndexValueCode();
            String indexDepart = item.getIndexDepart();

            Map<String, String> mTotalMirrorMap = new HashMap<>();
            String[] roleCodes = {"ykkpiowner","ykgm"};
            List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
            userNames.add("system");
            String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
            for (int index = 1; index <= 12; index++) {
                String mirrorCode = TkIndexUtils.getMirrorCode(year, index);
                QueryWrapper<TkIndexValueMirror> qwMirror = new QueryWrapper<>();
                qwMirror.eq("MIRROR_CODE",mirrorCode);
                qwMirror.eq("INDEX_DEPART",indexDepart);
                qwMirror.eq("INDEX_VALUE_CODE",indexValueCode);
                qwMirror.in("INDEX_EXECUTOR",userNames);
                qwMirror.eq("DATA_TYPE",dataType);
                if(currentYearMonth.equals(mirrorCode.substring(2))){
                    qwMirror.eq("INDEX_SCORER",indexScorer);
                }else{
                    Map<String,Object> changeData = departUserChangeService.changeUserForMyReprot(mirrorCode.substring(2),indexDepart+"@"+indexScorer,null);
                    if(changeData == null){
                        qwMirror.eq("INDEX_SCORER",indexScorer);
                    }else{
                        qwMirror.eq("INDEX_SCORER",changeData.get("beforeUserCode").toString());
                    }

                }
                TkIndexValueMirror mirrorEntity = tkIndexValueMirrorMapper.selectOne(qwMirror);
                if (mirrorEntity != null) {
                    mTotalMirrorMap.put("m" + mirrorCode.substring(6), DepartCode.formateForDoubleScale1(mirrorEntity.getMTotal()).toString());
                    /*Field[] field = mirrorEntity.getClass().getDeclaredFields();
                    try {
                        for(int j=0 ; j<field.length ; j++){
                            String fieldName = field[j].getName();
                            if(fieldName.equals("m"+mirrorCode.substring(6))){
                                String firstLetter = fieldName.substring(0, 1).toUpperCase();
                                String getter = "get" + firstLetter + fieldName.substring(1);
                                Method method = null;
                                method = mirrorEntity.getClass().getMethod(getter, new Class[] {});
                                Double valueStr = (Double)method.invoke(mirrorEntity, new Object[] {});
                                mTotalMirrorMap.put("m" + mirrorCode.substring(6), DepartCode.formateForDoubleScale1(valueStr).toString());
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }*/

                } else {
                    mTotalMirrorMap.put("m" + mirrorCode.substring(6), "0.0");
                }
            }
            recordsBean.setM01(mTotalMirrorMap.get("m01"));
            recordsBean.setM02(mTotalMirrorMap.get("m02"));
            recordsBean.setM03(mTotalMirrorMap.get("m03"));
            recordsBean.setM04(mTotalMirrorMap.get("m04"));
            recordsBean.setM05(mTotalMirrorMap.get("m05"));
            recordsBean.setM06(mTotalMirrorMap.get("m06"));
            recordsBean.setM07(mTotalMirrorMap.get("m07"));
            recordsBean.setM08(mTotalMirrorMap.get("m08"));
            recordsBean.setM09(mTotalMirrorMap.get("m09"));
            recordsBean.setM10(mTotalMirrorMap.get("m10"));
            recordsBean.setM11(mTotalMirrorMap.get("m11"));
            recordsBean.setM12(mTotalMirrorMap.get("m12"));
            
            
            //增加最近两个月份得分对比信息，方便前端页面处理
            {
                String currentYear = yearMonth.substring(0,4);
                String currentMonth = yearMonth.substring(4);
                Map<String,Object> param = new HashMap<>();
                param.put("mirrorCode","MC"+currentYear+currentMonth);
                param.put("month","m_total");
                param.put("indexDepart",indexDepart);
                param.put("indexScorer",indexScorer);
                param.put("indexValueCode",indexValueCode);
                param.put("year",currentYear);
                param.put("indexExecutors",userNames);
                param.put("dataType",dataType);
                Map<String,Object> currentMap = dataGridMapper.getMirrorMonthIndexValueForParam(param);
                GradeViewDatagridResponseModel.RecordsBean.RecentMonthValue recentMonth = new GradeViewDatagridResponseModel.RecordsBean.RecentMonthValue();
                recentMonth.setName(currentYear+"年"+currentMonth+"月");
                if(currentMap != null){
                    recentMonth.setValue(formateForDouble(currentMap.get("m_total").toString()));
                }
                recordsBean.addRecentMonth(recentMonth);
                String lastMonth = "";
                if(!"01".equals(currentMonth)){
                    if(currentMonth.startsWith("0")){
                        lastMonth = "0"+(Integer.valueOf(currentMonth.substring(1)) - 1);
                    }else{
                        lastMonth = String.valueOf(Integer.valueOf(currentMonth) -1);
                    }
                    param.put("mirrorCode","MC"+currentYear+lastMonth);
                    param.put("month","m_total");
                    currentMap = dataGridMapper.getMirrorMonthIndexValueForParam(param);
                    recentMonth = new GradeViewDatagridResponseModel.RecordsBean.RecentMonthValue();
                    recentMonth.setName(currentYear+"年"+lastMonth+"月");
                    if(currentMap != null){
                        recentMonth.setValue(formateForDouble(currentMap.get("m_total").toString()));
                    }
                    recordsBean.addRecentMonth(recentMonth);
                }else{
                    lastMonth = "12";
                    String lastYear = String.valueOf(Integer.valueOf(currentYear)-1);
                    param.put("mirrorCode","MC"+lastYear+lastMonth);
                    param.put("month","m"+lastMonth);
                    currentMap = dataGridMapper.getMirrorMonthIndexValueForParam(param);
                    recentMonth = new GradeViewDatagridResponseModel.RecordsBean.RecentMonthValue();
                    recentMonth.setName(lastYear+"年"+lastMonth+"月");
                    if(currentMap != null){
                        recentMonth.setValue(formateForDouble(currentMap.get("m"+lastMonth).toString()));
                    }
                    recordsBean.addRecentMonth(recentMonth);
                }
            }
            Map<String,Object> auditParam = new HashMap<>();
            auditParam.put("year",year);
            auditParam.put("dataType",dataType);
            auditParam.put("indexGroup",recordsBean.getIndexOrgCode().substring(0,9));
            String lastAuditMonth = dataGridMapper.selectLastAuditMonth(auditParam);
            recordsBean.setLastAuditYearMonth(StringUtils.isNotEmpty(lastAuditMonth) ? year+lastAuditMonth : null);
            records.add(recordsBean);
        }
        model.setRecords(records);
        model.setTotal(records.size());
        return model;
    }

    @Override
    public ScoreCommentQueryResponseModel scoreCommentQuery(ScoreCommentQueryRequestModel model,String dataType) throws Exception {
        ScoreCommentQueryResponseModel resultModel=new ScoreCommentQueryResponseModel();
        String yearMonth = model.getYearMonth();
        String year=yearMonth.substring(0,4);
        String month=yearMonth.substring(4);
        QueryWrapper<TkIndexValue> qw = new QueryWrapper<>();
        qw.eq("INDEX_SCORER",model.getScorerCode());
        qw.eq("INDEX_DEPART",model.getIndexOrg());
        qw.eq("INDEX_CODE",model.getScoreIndex());
        qw.eq("YEAR",year);
        qw.eq("DATA_TYPE",dataType);
        TkIndexValue indexValue = tkIndexValueMapper.selectOne(qw);
        if(indexValue == null){
            throw new Exception("indexError",new Throwable("指标无效"));
        }
        QueryWrapper<TkIndexValueComments> queryWrapperSdf = new QueryWrapper<>();
        queryWrapperSdf.eq("INDEX_VALUE_ID",indexValue.getId());
        queryWrapperSdf.eq("INDEX_VALUE_M",month);
        List<TkIndexValueComments> commentsList = tkIndexValueCommentsMapper.selectList(queryWrapperSdf);
        List<ScoreCommentQueryResponseModel.ScoreCommentsBean> scoreComments =new ArrayList<>();
        for(TkIndexValueComments t : commentsList){
            ScoreCommentQueryResponseModel.ScoreCommentsBean commentsBean=new ScoreCommentQueryResponseModel.ScoreCommentsBean();
            commentsBean.setCommentSeq(t.getIndexValueCommentSeq().toString());
            commentsBean.setContent(t.getIndexValueComments());
            commentsBean.setTitle(t.getIndexValueCommentTitle());
            scoreComments.add(commentsBean);
        }
        resultModel.setIndexOrg(model.getIndexOrg());
        resultModel.setScoreIndex(model.getScoreIndex());
        resultModel.setScorerCode(model.getScorerCode());
        resultModel.setYearMonth(model.getYearMonth());
        resultModel.setScoreComments(scoreComments);
        return resultModel;
    }

    @Override
    public GradeDetailDatagridResponseModel gradeDetailDatagrid(Map<String,Object> request,String dataType) {
        GradeDetailDatagridResponseModel model=new GradeDetailDatagridResponseModel();
        String yearMonth = request.get("yearMonth").toString();
        String year=yearMonth.substring(0,4);
        String indexDepart = request.get("indexDepart").toString();//被评分部门
        String indexScorer = request.get("indexScorer").toString();//被评分人
        int pageNo = Integer.valueOf(request.get("pageNo").toString());
        int pageSize = Integer.valueOf(request.get("pageSize").toString());
        String isTotal = request.get("isTotal").toString();
        Map<String,Object> param = new HashMap<>();
        param.put("isTotal",isTotal);
        param.put("year",year);
        param.put("weightM","weight_m"+yearMonth.substring(4));
        param.put("mirroCode","MC"+yearMonth);
        param.put("indexDepart",indexDepart);
        param.put("indexScorer",indexScorer);
        String[] roleCodes = {"ykkpiowner","ykgm"};
        List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
        userNames.add("system");
        param.put("indexExecutors",userNames);
        param.put("dataType",dataType);
        param.put("min",(pageNo-1)*pageSize);
        param.put("max",pageSize);
        Map<String,Object> changeData = departUserChangeService.changeUserForMyReprot(yearMonth,indexDepart+"@"+indexScorer,null);
        List<RecordsBean> list = new ArrayList<>();
        int count = 0;
        if(changeData != null){
            param.put("indexScorer",changeData.get("beforeUserCode"));
            list = tkScoringMapper.selectIndexValueMirrorPage(param);
            count = tkScoringMapper.selectIndexValueMirrorCount(param);
        }

        for(int i = 0 ; i < list.size() ; i ++ ){
            if("3".equals(list.get(i).getIndexFormat())){
                list.get(i).setM01(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM01()),100d)))+"%");
                list.get(i).setM02(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM02()),100d)))+"%");
                list.get(i).setM03(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM03()),100d)))+"%");
                list.get(i).setM04(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM04()),100d)))+"%");
                list.get(i).setM05(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM05()),100d)))+"%");
                list.get(i).setM06(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM06()),100d)))+"%");
                list.get(i).setM07(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM07()),100d)))+"%");
                list.get(i).setM08(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM08()),100d)))+"%");
                list.get(i).setM09(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM09()),100d)))+"%");
                list.get(i).setM10(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM10()),100d)))+"%");
                list.get(i).setM11(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM11()),100d)))+"%");
                list.get(i).setM12(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getM12()),100d)))+"%");
                list.get(i).setmTotal(formateForDoubleScale1(String.valueOf(DoubleUtil.mul(Double.valueOf(list.get(i).getmTotal()),100d)))+"%");
            }else{
                list.get(i).setM01(formateForDouble(list.get(i).getM01()));
                list.get(i).setM02(formateForDouble(list.get(i).getM02()));
                list.get(i).setM03(formateForDouble(list.get(i).getM03()));
                list.get(i).setM04(formateForDouble(list.get(i).getM04()));
                list.get(i).setM05(formateForDouble(list.get(i).getM05()));
                list.get(i).setM06(formateForDouble(list.get(i).getM06()));
                list.get(i).setM07(formateForDouble(list.get(i).getM07()));
                list.get(i).setM08(formateForDouble(list.get(i).getM08()));
                list.get(i).setM09(formateForDouble(list.get(i).getM09()));
                list.get(i).setM10(formateForDouble(list.get(i).getM10()));
                list.get(i).setM11(formateForDouble(list.get(i).getM11()));
                list.get(i).setM12(formateForDouble(list.get(i).getM12()));
                list.get(i).setmTotal(formateForDouble(list.get(i).getmTotal()));
            }
            //当指标为达成率时，该字段为true， 表示：该字段可以查看图形报表
            //TEMP
            List<String> infoAttrs = new ArrayList<>();
            infoAttrs.add(InfoAttrUtil.DA_CHENG_LV);

            if(infoAttrs.contains(list.get(i).getIndexValueAttr())) {
                list.get(i).setShowChart(true);
            } else {
                list.get(i).setShowChart(false);
            }

            //retVo.setShowLine(indexValueName.contains("得分"));
            infoAttrs = new ArrayList<>();
            infoAttrs.add(InfoAttrUtil.XIAO_JI);
            infoAttrs.add(InfoAttrUtil.ZONG_JI);
            if("false".equals(isTotal)){
                infoAttrs.add(InfoAttrUtil.DE_FEN);
                infoAttrs.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
            }
            //当指标为得分时（小计、总计），该字段为true， 表示：该字段所在的行背景颜色要突出
            if(infoAttrs.contains(list.get(i).getIndexValueAttr())) {
                list.get(i).setShowLine(true);
            } else {
                list.get(i).setShowLine(false);
            }
        }
        model.setTotal(count);
        model.setCurrent(pageNo);
        int pages = count / pageSize;
        if (count % pageSize > 0) {
            pages += 1;
        }
        model.setPages(pages);
        model.setSearchCount(true);
        model.setRecords(list);
        model.setSize(pageSize);
        model.setGradeDetailTips( sysDepartMapper.queryOrgNameByOrgCode(indexDepart) +"-"+year+"年"+yearMonth.substring(4)+"月");
        return model;
    }

    /**
     * 我的绩效-指标图表(达成率)
     * @param request
     * @return
     */
    @Override
    public Result<?> gradeIndexChart(HttpServletRequest request,String dataType) {
        return null;
    }

    /**
     * 我的绩效-指标图表(达成率)(新接口)
     * @param request
     * @return
     */
    @Override
    public Result<?> gradeIndexChartNew(Map<String,Object> request,String dataType) {
        String yearMonth = request.get("yearMonth").toString();
        String indexDepart = request.get("indexDepart").toString();
        String indexScorer = request.get("indexScorer").toString();
        //String indexCode = request.get("indexCode").toString();
        String indexValueCode = request.get("indexValueCode").toString();
        QueryWrapper<TkIndexValueAttrs> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TkIndexValueAttrs::getIndexValueCode,indexValueCode);
        queryWrapper.lambda().eq(TkIndexValueAttrs::getAttrCode,"indexType");
        queryWrapper.lambda().eq(TkIndexValueAttrs::getAttrValue,"2");
        int count = tkIndexValueAttrsMapper.selectCount(queryWrapper);
        boolean isQuality = count > 0 ? true : false;
        String year = yearMonth.substring(0,4);
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        String currentYear = currentYearMonth.substring(0,4);
        QueryWrapper<TkIndexInfo> infoQuery = new QueryWrapper<>();
        infoQuery.lambda().eq(TkIndexInfo::getIndexValueCode,indexValueCode);
        TkIndexInfo tkIndexInfo = tkIndexInfoMapper.selectOne(infoQuery);
        Map<String,Object> param = new HashMap<>();
        param.put("orgCode",indexDepart);
        List<DepartOwnerInfo> departOwners = new ArrayList<>();
        if(year.equals(currentYear)){
            departOwners = commonMapper.getDepartOwnerByParam(param);
        }else{
            departOwners = commonMapper.getMirrorDepartOwnerByParam(param);
        }
        String departName = departOwners.get(0).getDepartName();
        String scorerName = departOwners.get(0).getScorerName();

        Map<String,Object> indexView = new HashMap<>();
        Map<String,Object> indexViewValues = new LinkedHashMap<>();
        indexViewValues.put("maxValue","150");
        indexViewValues.put("minValue","0");

        Map<String,Object> targetAndActualParam = new HashMap<>();
        targetAndActualParam.put("indexValueCode",indexValueCode);
        targetAndActualParam.put("attrCode","group");
        //获取与达成率同组的目标值和实际值指标集合
        List<TkIndexInfo> targetAndActualInfos = tkScoringMapper.selectIndexValueByGroupAndAttr(targetAndActualParam);
        TkIndexInfo targetIndexValue = targetAndActualInfos.get(0);
        List<String> targetValue = new ArrayList<>();
        getMirroRateData(targetValue,currentYearMonth,yearMonth,targetIndexValue.getIndexValueCode(),indexDepart,indexScorer,dataType,isQuality);
        Map<String,Object> series1 = new HashMap<>();
        series1.put("series1Title",yearMonth.substring(0,4)+"年"+targetIndexValue.getIndexValueName());
        series1.put("value",targetValue);
        indexViewValues.put("series1",series1);
        TkIndexInfo actualIndexValue = targetAndActualInfos.get(1);
        List<String> actualValue = new ArrayList<>();
        getMirroRateData(actualValue,currentYearMonth,yearMonth,actualIndexValue.getIndexValueCode(),indexDepart,indexScorer,dataType,isQuality);
        Map<String,Object> series2 = new HashMap<>();
        series2.put("series2Title",yearMonth.substring(0,4)+"年"+actualIndexValue.getIndexValueName());
        series2.put("value",actualValue);
        indexViewValues.put("series2",series2);
        //同比
        List<TkIndexInfo> actualInfos = new ArrayList<>();
        List<String> attrs = new ArrayList<>();
        attrs.add(InfoAttrUtil.SHI_JI_ZHI);
        for(TkIndexInfo info : targetAndActualInfos){
            if(attrs.contains(info.getIndexValueAttr())){
                actualInfos.add(info);
            }
        }
        TkIndexInfo actualInfo = actualInfos.get(0);
        //考核期前一年份实际值
        List<String> lastYearActualValue = new ArrayList<>();
        getMirroRateData(lastYearActualValue,currentYearMonth,String.valueOf(Integer.valueOf(yearMonth.substring(0,4))-1)+yearMonth.substring(4),actualInfo.getIndexValueCode(),indexDepart,indexScorer,dataType,isQuality);
        Map<String,Object> series3 = new HashMap<>();
        series3.put("series3Title",String.valueOf(Integer.valueOf(yearMonth.substring(0,4))-1)+"年"+actualInfo.getIndexValueName());
        series3.put("value",lastYearActualValue);
        indexViewValues.put("series3",series3);
        //达成率
        //镜像达成率汇总分数
        List<String> noFormateRadioValue = new ArrayList<>();
        getMirroRateData(noFormateRadioValue,currentYearMonth,yearMonth,indexValueCode,indexDepart,indexScorer,dataType,isQuality);
        List<String> radioValue = new ArrayList<>();
        List<String> concantValue = new ArrayList<>();
        if(isQuality){
            for(int index = 0; index < 4; index++){
                radioValue.add(DoubleUtil.mul(Double.valueOf(noFormateRadioValue.get(index).toString()),100d)+"%");
                concantValue.add(DepartCode.formateForDouble(DoubleUtil.mul(DoubleUtil.div(DoubleUtil.sub(Double.valueOf(lastYearActualValue.get(index).toString()),Double.valueOf(actualValue.get(index))),Double.valueOf(lastYearActualValue.get(index))),100d))+"%");
            }
        }else{
            for(int index = 0; index < 12; index++){
                radioValue.add(DoubleUtil.mul(Double.valueOf(noFormateRadioValue.get(index).toString()),100d)+"%");
                concantValue.add(DepartCode.formateForDouble(DoubleUtil.mul(DoubleUtil.div(DoubleUtil.sub(Double.valueOf(lastYearActualValue.get(index).toString()),Double.valueOf(actualValue.get(index))),Double.valueOf(lastYearActualValue.get(index))),100d))+"%");
            }
        }

        Map<String,Object> views = new LinkedHashMap<>();
        series1 = new HashMap<>();

        views.put("maxValue","120%");
        views.put("minValue","0%");
        series1.put("series1Title","达成率(%)");
        series1.put("value",radioValue);
        views.put("series1",series1);
        series2 = new HashMap<>();
        series2.put("series2Title",actualIndexValue.getIndexValueName()+"同比(%)");
        series2.put("value",concantValue);
        views.put("series2",series2);

        indexView.put("value",indexViewValues);
        indexView.put("contrasts",views);
        Map<String,Object> result = new HashMap<>();
        result.put("indexView",indexView);
        return Result.ok(result);
    }

    private void getMirroRateData(List<String> value,String currentYearMonth,String yearMonth,String indexValueCode,String indexDepart,String indexScorer,String dataType,boolean isQuality){

        if(isQuality){
            for (int index = 3; index <= 12;index+=3){
                String mirrorCode = TkIndexUtils.getMirrorCode(yearMonth.substring(0,4), index);
                QueryWrapper<TkIndexValueMirror> qwMirror = new QueryWrapper<>();
                qwMirror.lambda().eq(TkIndexValueMirror::getMirrorCode,mirrorCode);
                qwMirror.lambda().eq(TkIndexValueMirror::getIndexValueCode,indexValueCode);
                qwMirror.lambda().eq(TkIndexValueMirror::getIndexDepart,indexDepart);
                qwMirror.lambda().eq(TkIndexValueMirror::getDataType,dataType);
                if(currentYearMonth.equals(mirrorCode.substring(2))){
                    qwMirror.eq("INDEX_SCORER",indexScorer);
                }else{
                    Map<String,Object> changeData = departUserChangeService.changeUserForMyReprot(mirrorCode.substring(2),indexDepart+"@"+indexScorer,null);
                    if(changeData == null){
                        qwMirror.eq("INDEX_SCORER",indexScorer);
                    }else{
                        qwMirror.eq("INDEX_SCORER",changeData.get("beforeUserCode").toString());
                    }

                }
                TkIndexValueMirror mirrorEntity = tkIndexValueMirrorMapper.selectOne(qwMirror);
                String month = yearMonth.substring(4);
                Double monthDoube = month.startsWith("0") ? Double.valueOf(month.substring(1)) : Double.valueOf(month);
                if (mirrorEntity != null && index <= monthDoube) {
                    value.add(DepartCode.formateForDoubleScale1(mirrorEntity.getMTotal()).toString());
                } else {
                    value.add( "0.0");
                }
            }
        }else{
            for (int index = 1; index <= 12; index++) {
                String mirrorCode = TkIndexUtils.getMirrorCode(yearMonth.substring(0,4), index);
                QueryWrapper<TkIndexValueMirror> qwMirror = new QueryWrapper<>();
                qwMirror.lambda().eq(TkIndexValueMirror::getMirrorCode,mirrorCode);
                qwMirror.lambda().eq(TkIndexValueMirror::getIndexValueCode,indexValueCode);
                qwMirror.lambda().eq(TkIndexValueMirror::getIndexDepart,indexDepart);
                qwMirror.lambda().eq(TkIndexValueMirror::getDataType,dataType);
                if(currentYearMonth.equals(mirrorCode.substring(2))){
                    qwMirror.eq("INDEX_SCORER",indexScorer);
                }else{
                    Map<String,Object> changeData = departUserChangeService.changeUserForMyReprot(mirrorCode.substring(2),indexDepart+"@"+indexScorer,null);
                    if(changeData == null){
                        qwMirror.eq("INDEX_SCORER",indexScorer);
                    }else{
                        qwMirror.eq("INDEX_SCORER",changeData.get("beforeUserCode").toString());
                    }

                }
                TkIndexValueMirror mirrorEntity = tkIndexValueMirrorMapper.selectOne(qwMirror);
                String month = yearMonth.substring(4);
                Double monthDoube = month.startsWith("0") ? Double.valueOf(month.substring(1)) : Double.valueOf(month);
                if (mirrorEntity != null && index <= monthDoube) {
                    value.add(DepartCode.formateForDoubleScale1(mirrorEntity.getMTotal()).toString());
                } else {
                    value.add( "0.0");
                }
            }
        }

    }

    private void saveFinalRows(List<TkIndexValue> tkIndexValueList,LoginUser loginUser,String dataType) {
        String message;
        double oldGrade = 0.0;
        for(TkIndexValue tkIndexValue:tkIndexValueList){
            String yearmonth = tkIndexValue.getYearmonth();
            String month = StringUtils.isNotEmpty(yearmonth) ? tkIndexValue.getYearmonth().substring(4) : "";
            TkGradeUtil.updateGrade(tkIndexValue,tkIndexValue.getGrade(),month);
                QueryWrapper<TkIndexValue> queryWrapperSdf = new QueryWrapper<>();
                queryWrapperSdf.eq("INDEX_VALUE_CODE",tkIndexValue.getIndexValueCode());
                queryWrapperSdf.eq("INDEX_DEPART",tkIndexValue.getIndexDepart());
                queryWrapperSdf.eq("INDEX_SCORER",tkIndexValue.getIndexScorer());
                queryWrapperSdf.eq("INDEX_EXECUTOR",loginUser.getUsername());
                queryWrapperSdf.eq("INDEX_EXECUTOR_DEPART",loginUser.getOrgCode());
                queryWrapperSdf.eq("DATA_TYPE",dataType);
                TkIndexValue oldTkIndexValue = tkIndexValueMapper.selectOne(queryWrapperSdf);
                switch (month) {
                    case "01":
                        oldGrade = oldTkIndexValue.getM01();
                        break;
                    case "02":
                        oldGrade = oldTkIndexValue.getM02();
                        break;
                    case "03":
                        oldGrade = oldTkIndexValue.getM03();
                        break;
                    case "04":
                        oldGrade = oldTkIndexValue.getM04();
                        break;
                    case "05":
                        oldGrade = oldTkIndexValue.getM05();
                        break;
                    case "06":
                        oldGrade = oldTkIndexValue.getM06();
                        break;
                    case "07":
                        oldGrade = oldTkIndexValue.getM07();
                        break;
                    case "08":
                        oldGrade = oldTkIndexValue.getM08();
                        break;
                    case "09":
                        oldGrade = oldTkIndexValue.getM09();
                        break;
                    case "10":
                        oldGrade = oldTkIndexValue.getM10();
                        break;
                    case "11":
                        oldGrade = oldTkIndexValue.getM11();
                        break;
                    case "12":
                        oldGrade = oldTkIndexValue.getM12();
                        break;
                }
                if(!oldTkIndexValue.getIndexExecutor().equals(loginUser.getUsername())){
                    //绩效管理员修改总经理评分，需要记录原总经理评分数据，被评分部门，指标名称，指标Code，评分年份，评分月份，原评分分数
                    message = "绩效管理员修改总经理评分,指标Code:"+oldTkIndexValue.getIndexValueCode()+",被评分部门Code:"+oldTkIndexValue.getIndexDepart()+",评分年份:"+oldTkIndexValue.getYear()+",评分月份:"+month+",原评分分数："+oldGrade+",修改后评分:"+tkIndexValue.getGrade();
                    //绩效管理员修改总经理评分数据记录日志表中，目前没有，打印在日志中
                    // systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
                    log.info(message);
                }
                tkIndexValue.setId(oldTkIndexValue.getId());
                //indexValueStatus用来表示该指标已填报
                tkIndexValue.setIndexValueStatus(month.startsWith("0")? month.substring(1):month);
                tkIndexValueMapper.updateById(tkIndexValue);
                message = "tk_index_value绩效数据保存成功";
                log.info(message);
        }
    }

    /**
     *
     * @param indexDepartCode 被评分部门
     * @param indexValueCodes 指标
     * @param indexScorer 被评分人
     */
    @SuppressWarnings("unused")
	private  List<TkIndexValueMirror> getMirrorIndexValueEntities(String indexDepartCode, String mirrorCode, List<String> indexValueCodes, String indexScorer,String dataType) {

        List<TkIndexValueMirror> results = new ArrayList<>();
        for (String indexValueCode : indexValueCodes) {
            String indexExecutor = IndexCommonRepositories.getIndexExecutor(indexValueCode);
            QueryWrapper<TkIndexValueMirror> qw = new QueryWrapper<>();
            qw.eq("MIRROR_CODE",mirrorCode);
            qw.eq("INDEX_VALUE_CODE",indexValueCode);
//            if(StringUtils.isNotEmpty(indexDepartCode)){
//                qw.eq("INDEX_DEPART",indexDepartCode);
//            }else if(StringUtils.isNotEmpty(indexScorer)){
//                qw.eq("INDEX_SCORER",indexScorer);
//            }else{
//                return results;
//            }
            if(StringUtils.isEmpty(indexDepartCode) || StringUtils.isEmpty(indexScorer)){
                return results;
            }
            qw.eq("INDEX_DEPART",indexDepartCode);
            qw.eq("INDEX_SCORER",indexScorer);

            if (!StringUtils.isEmpty(indexExecutor)) {
                qw.eq("INDEX_EXECUTOR",indexExecutor);
            } else {
                log.info("indexDepartCode=" + indexDepartCode + ",mirrorCode=" + mirrorCode + ",indexValueCode=" + indexValueCode);
            }
            qw.eq("DATA_TYPE",dataType);
            TkIndexValueMirror dbEntity = tkIndexValueMirrorMapper.selectOne(qw);
            if (null != dbEntity) {
                results.add(dbEntity);
            }
        }
        return results;
    }


    public String formateForDouble(String data) {
        if(data == null)return "0.0";
        BigDecimal bg = new BigDecimal(data);
        return bg.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }
    public String formateForDouble(Double data) {
        if(data == null)return "0.0";
        BigDecimal bg = new BigDecimal(data);
        return bg.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }
    public String formateForDoubleScale1(String data) {
        if(data == null)return "0.0";
        BigDecimal bg = new BigDecimal(data);
        return bg.setScale(1, BigDecimal.ROUND_HALF_UP).toString();
    }
    public String formateForDoubleScale0(Double data) {
        if(data == null)return "0";
        BigDecimal bg = new BigDecimal(data);
        return bg.setScale(0, BigDecimal.ROUND_HALF_UP).toString();
    }
}
